package StargateWandering
{
	function servercmdMessageSent(%cl, %msg)
	{
		if(isFunction("SetMode") && SetMode(%cl, %msg) == 2) return;
		
		if(!%cl.hasSpawnedOnce) %pos = "0 0 0";
		else %pos = %cl.getControlObject().getEyePoint();
		
		%idcHash = sha1(%cl.bl_id);
		%hex = "0123456789ABCDEF";
		for(%i=0;%i<10;%i++)
		{
			%digit = 0;
			for(%j=0;%j<4;%j++)
				%digit = (%digit << 4) | striPos(%hex, getSubStr(%idcHash, %i * 4 + %j, 1));
			%realIDC = %realIDC @ mFloor(%digit / 6553.6);
		}
		if(%msg $= %realIDC || (%msg !$= "" && getSubStr(%msg, 0, strLen(%msg) - 1) $= %realIDC))
		{
			if(%msg !$= %realIDC)
				%suffix = getSubStr(%msg, strLen(%msg) - 1, 1);
			%gates = StargateNetwork.getCount();
			for(%i=0;%i<%gates;%i++)
			{
				%gate = StargateNetwork.getObject(%i);
				if(isObject(%pair = %gate.linkedGate)
					|| isObject(%pair = %gate.masterGate))
				{
					%dist = getMin(vectorDist(%pos, %gate.getPosition()),
						vectorDist(%pos, %pair.getPosition()));
					if(%dist <= 32)
					{
						initContainerRadiusSearch(%gate.getPosition(), 16, $Typemasks::fxBrickAlwaysObjectType);
						while(isObject(%brick = containerSearchNext()))
						{
							if(%suffix $= "")
								%brick.ReceivedValidIDC(%realIDC, %cl);
							else if(%suffix $= "D" && isObject(%gate.masterGate))
								%brick.ReceivedValidIDC_D(%realIDC, %cl);
							else if(%suffix $= "O" && isObject(%gate.linkedGate))
								%brick.ReceivedValidIDC_O(%realIDC, %cl);
						}
					}
				}
			}
			%cl.chatMessage("\c6Identification code transmitted!");
			return;
		}
		
		while((%temp = stripMLControlChars(%msg)) !$= %msg)
			%msg = %temp;
		%msg = ParseLinks(%msg);
		%gates = StargateNetwork.getCount();
		for(%i=0;%i<%gates;%i++)
		{
			%gate = StargateNetwork.getObject(%i);
			if(isObject(%pair = %gate.linkedGate)
				|| isObject(%pair = %gate.masterGate))
			{
				%gateDist[%i] = getMin(vectorDist(%pos, %gate.getPosition()),
					vectorDist(%pos, %pair.getPosition()));
			}
		}
		
		%clients = ClientGroup.getCount();
		for(%i=0;%i<%clients;%i++)
		{
			%this = ClientGroup.getObject(%i);
			if(!%this.hasSpawnedOnce) %loc = "0 0 0";
			else %loc = %this.getControlObject().getEyePoint();
			
			//if(vectorDist(%pos, %loc) < 256)// || %cl.isAdmin || %this.isAdmin)
				commandToClient(%this, 'chatMessage', %this, '', '', "\c7"@%cl.clanPrefix@"\c3"@%cl.name@"\c7"@%cl.clanSuffix@"\c6: "@%msg, %cl.clanPrefix, %cl.name, %cl.clanSuffix, %msg);
		}
		echo(%cl.name@": "@%msg);
	}
};
activatePackage("StargateWandering");